package jmine.tec.report.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import jmine.tec.report.api.RendererParameters;

/**
 * Entidade que armazena os parâmetros para os renderizadores.
 * 
 * @author lundberg
 */
public class RendererParametersImpl implements RendererParameters, Serializable {

    private RendererParameters parent;

    private Map<String, String> parameters;

    /**
     * Construtor
     */
    public RendererParametersImpl() {
        this(null);
    }

    /**
     * Construtor
     * 
     * @param parent parent
     */
    public RendererParametersImpl(RendererParameters parent) {
        this.parent = parent;
        this.parameters = new HashMap<String, String>();
    }

    /**
     * {@inheritDoc}
     */
    public String getString(String key) {
        if (this.parameters.containsKey(key)) {
            return this.parameters.get(key);
        } else if (this.parent != null) {
            return this.parent.getString(key);
        } else {
            return null;
        }
    }

    /**
     * {@inheritDoc}
     */
    public String getString(String key, String defaultValue) {
        String value = this.getString(key);
        return value == null ? defaultValue : value;
    }

    /**
     * {@inheritDoc}
     */
    public Long getLong(String key) {
        String value = this.getString(key);
        return value == null ? null : Long.valueOf(value);
    }

    /**
     * {@inheritDoc}
     */
    public Long getLong(String key, Long defaultValue) {
        Long value = this.getLong(key);
        return value == null ? defaultValue : value;
    }

    /**
     * {@inheritDoc}
     */
    public Boolean getBoolean(String key) {
        String value = this.getString(key);
        return value == null ? null : Boolean.valueOf(value);
    }

    /**
     * {@inheritDoc}
     */
    public Boolean getBoolean(String key, Boolean defaultValue) {
        Boolean value = this.getBoolean(key);
        return value == null ? defaultValue : value;
    }

    /**
     * {@inheritDoc}
     */
    public void put(String key, String value) {
        this.parameters.put(key, value);
    }

    /**
     * {@inheritDoc}
     */
    public void setParent(RendererParameters parent) {
        this.parent = parent;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public RendererParameters clone() {
        RendererParametersImpl clone;
        try {
            clone = (RendererParametersImpl) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new IllegalStateException();
        }
        clone.parameters = new HashMap<String, String>(this.parameters);
        return clone;
    }

    /**
     * {@inheritDoc}
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        String raw = this.getString(key);
        return raw == null ? defaultValue : Boolean.parseBoolean(raw);
    }

    /**
     * {@inheritDoc}
     */
    public long getLong(String key, long defaultValue) {
        String raw = this.getString(key);
        return raw == null ? defaultValue : Long.parseLong(raw);
    }

    /**
     * {@inheritDoc}
     */
    public Float getFloat(String key) {
        String value = this.getString(key);
        return value == null ? null : Float.valueOf(value);
    }

    /**
     * {@inheritDoc}
     */
    public Float getFloat(String key, Float defaultValue) {
        Float value = this.getFloat(key);
        return value == null ? defaultValue : value;
    }

    /**
     * {@inheritDoc}
     */
    public float getFloat(String key, float defaultValue) {
        String raw = this.getString(key);
        return raw == null ? defaultValue : Float.parseFloat(raw);
    }

    /**
     * {@inheritDoc}
     */
    public Integer getInteger(String key) {
        String value = this.getString(key);
        return value == null ? null : Integer.valueOf(value);
    }

    /**
     * {@inheritDoc}
     */
    public Integer getInteger(String key, Integer defaultValue) {
        Integer value = this.getInteger(key);
        return value == null ? defaultValue : value;
    }

    /**
     * {@inheritDoc}
     */
    public int getInteger(String key, int defaultValue) {
        String raw = this.getString(key);
        return raw == null ? defaultValue : Integer.parseInt(raw);
    }
}
